package com.buss.product.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;
import com.buss.product.model.CatePropGroup;
import com.buss.product.service.CatePropGroupService;

@Controller
@RequestMapping("/admin/productprop")
public class CatePropGroupController extends BaseController {

	@Autowired
	private CatePropGroupService groupService;
	
	@Override
	@RequestMapping("/page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("productprop/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@Override
	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, Long id) {
		ModelAndView view = new ModelAndView("/productprop/edit");
		CatePropGroup catePropGroup = new CatePropGroup();
		if(EDIT_PAGE.equals(type)){			
			catePropGroup = groupService.loadPropGroup(id);
		}else{
			catePropGroup.setStatus(RecordStatus.normal);
		}			
		view.addObject("type", type);
		view.addObject("catePropGroup", catePropGroup);
		return view;
	}

	@Override
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		groupService.removePropGroup(ids);
		return SysStaticParam.resultMessage("删除属性分组信息成功!");
	}

	/**
	 * 新增属性分组信息
	 * @param propGroup
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( CatePropGroup propGroup,HttpServletRequest request){
		propGroup.setCompanyId(getCompanyId(request));
		propGroup = groupService.savePropGroup(propGroup);
		Map<String, Object> params = SysStaticParam.resultMessage("新增属性分组信息成功!");
		params.put("catePropGroup", propGroup);
		return params;		
	}
	
	/**
	 * 修改属性分组信息
	 * @param propGroup
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit( CatePropGroup propGroup,HttpServletRequest request){
		propGroup.setCompanyId(getCompanyId(request));
		groupService.editPropGroup(propGroup);
		return SysStaticParam.resultMessage("修改属性分组信息成功!");
	}
	
	/**
	 * 获取属性分组列表信息
	 * @param name
	 * @param status
	 * @param orderField
	 * @param orderType
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String name,RecordStatus status,String orderField,String orderType,Pagination page,HttpServletRequest request){
		OrderBy orderBy = null;
		if(StringUtil.isNotEmpty(orderField)){
			orderBy = new OrderBy();
			orderBy.add(orderField, orderType);
		}
		ResultList<CatePropGroup> rs = groupService.queryCatePropGroup(getCompanyId(request), name, status, orderBy, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取属性分组信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}

	/**
	 * 根据分类编码获取对应的属性分组信息列表
	 * @param categoryId
	 * @param status
	 * @param orderField
	 * @param orderType
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list/{categoryId}")
	@ResponseBody
	public Map<String,Object> listByCategoryId(@PathVariable Long categoryId,RecordStatus status,String orderField,String orderType,Pagination page,HttpServletRequest request){
		OrderBy orderBy = null;
		if(StringUtil.isNotEmpty(orderField)){
			orderBy = new OrderBy();
			orderBy.add(orderField, orderType);
		}
		ResultList<CatePropGroup> rs = groupService.queryCatePropGroupByParentCategoryId(getCompanyId(request), categoryId, status, orderBy, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取属性分组信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
}
